package com.mofang.powerdekorhelper.base.recycleview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mofang.powerdekorhelper.utils.GlideLoader;

/**
 * Created by luofei on 2016/9/19 0019.
 */
public class BaseViewHolder extends RecyclerView.ViewHolder {
    View convertView;
    Context context;

    public BaseViewHolder(View itemView, Context context) {
        super(itemView);
        this.convertView = itemView;
        this.context = context;
    }

    public void setText(int id, String text) {
        TextView textView = (TextView) convertView.findViewById(id);
        textView.setText(text);
    }

    public void setTextColor(int id, int color) {
        TextView textView = (TextView) convertView.findViewById(id);
        textView.setTextColor(context.getResources().getColor(color));
    }

    public void setTextBackground(int id, int drawId) {
        TextView textView = (TextView) convertView.findViewById(id);
        textView.setBackgroundResource(drawId);
    }

    public void setImage(int id, String url, int defaultImg) {

        ImageView imageView = (ImageView) convertView.findViewById(id);

        if (url != null && !url.equals("")) {
            Glide.with(context)
                    .load(url)
                    .placeholder(defaultImg)
                    //.error(defaultImg)
                    .into(imageView);

        } else {
            if (defaultImg != -1) {
                imageView.setBackgroundResource(defaultImg);
            }
        }
    }

    public void setFileImage(int id, String path, int defaultImg) {
        ImageView imageView = (ImageView) convertView.findViewById(id);
        if (path != null && !path.equals("")) {
            //imageView.setImageBitmap(ImageUtils.fitSizeImg(path));
        } else {
            imageView.setBackgroundResource(defaultImg);
        }
    }

    public void setCircleImage(int id, String url, int defaulImage) {
        ImageView imageView = (ImageView) convertView.findViewById(id);
        GlideLoader.getInstance(context).loadCircleImage(url, imageView, null);
    }

    public void setCircleImage2(int id, String url) {
        ImageView imageView = (ImageView) convertView.findViewById(id);
        //PicassoUtils.setCircleImageUrl(context, url, imageView);
    }

    public void setImageResouse(int id, int drawableId) {
        ImageView imageView = (ImageView) convertView.findViewById(id);
        imageView.setImageResource(drawableId);
    }


    public void setViewVisiable(int id, int isVisiable) {
        View view = convertView.findViewById(id);
        view.setVisibility(isVisiable);
    }

    public void setCheckBox(int id, boolean check) {
        CheckBox checkBox = (CheckBox) convertView.findViewById(id);
        if (check) {
            checkBox.setChecked(check);
        } else {
            checkBox.setChecked(false);
        }
    }

    public void setCheckBoxStateListener(int id, CompoundButton.OnCheckedChangeListener changeListener) {
        CheckBox checkBox = (CheckBox) convertView.findViewById(id);
        checkBox.setOnCheckedChangeListener(changeListener);
    }

    public void setOnclickListener(int id, View.OnClickListener onClickListener) {
        View view = convertView.findViewById(id);
        view.setOnClickListener(onClickListener);
    }

    public String getText(int id) {
        TextView textView = (TextView) convertView.findViewById(id);
        return textView.getText().toString();
    }

}
